﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

public partial class Users_UpdateAncestralHall : BasePage
{
    protected string DefaultValue = "";
    private AncestralHallBLL bll = new AncestralHallBLL();
    private SaleNumber salBll = new SaleNumber();


    protected string city;
    protected string province;

    protected int Id = 0;

    protected string AncestralHallId;

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx", "请您登录", true);

        if (!IsPostBack)
        {
            string Id = Request["id"];
            if (!string.IsNullOrEmpty(Id))
            {
                ViewState["id"] = Id;
                this.Id = StringHelper.isNumber_int(Id);
                GetOldAncestralHall();
                initPage();
            }
        }
        else
        { 
            if(ViewState["id"] !=null)
            {
                this.Id = StringHelper.isNumber_int(ViewState["id"].ToString());
            }
        }

    }


    public void GetOldAncestralHall()
    { 
        DataTable dataTable=new DataTable();
        if(ViewState["id"] !=null)
        {
            int id =StringHelper.isNumber_int(ViewState["id"].ToString());
            if (id > 0)
            {
                dataTable = bll.GetAncestralHallInfoById(id);
                ViewState["OldAncestralHall"] = dataTable;
            }
            
        }
    }

    public void initPage()
    {
        DataTable dataTable = new DataTable();
        dataTable = ViewState["OldAncestralHall"] as DataTable;
        if(dataTable!=null)
        {
            if(dataTable.Rows.Count>0)
            {
                txtAncestralHallId.Text = dataTable.Rows[0]["AncestralHallId"].ToString();
                txtName.Text = dataTable.Rows[0]["Name"].ToString();
                txtPreName.Text = dataTable.Rows[0]["PreName"].ToString();

                province = dataTable.Rows[0]["Province"].ToString();
                city = dataTable.Rows[0]["City"].ToString();

                WebEditor1.Text = dataTable.Rows[0]["Introduction"].ToString();

                

                Users_Model userModel = Base_GetUserInfo;
                if (userModel.UserId <= 0)
                {
                    ResultStr("AncestralHallList.aspx", "无访问权限!");
                }
                else
                {
                    if (userModel.UserId != StringHelper.isNumber_int(dataTable.Rows[0]["CreateId"].ToString()))
                    {
                        ResultStr("AncestralHallList.aspx", "该祠堂不是您创建的，无访问权限!");
                    }
                }
            }
        }
    }



    public AncestralHall_Model getModel()
    {
        Users_Model userModel = Base_GetUserInfo;

        AncestralHall_Model model = new AncestralHall_Model();
        string AncestralHallId = "";
        string Name = txtName.Text;
        string PreName = txtPreName.Text;
        //int CreateId = 0;

        int Province = 0;
        int City = 0;

        string Introduction = WebEditor1.Text;
        int MusicId = 0;
        int TempleId = 0;
        DateTime CreateTime = DateTime.Now;
        int IsCheck = 0;
        int isDel = 0;
        int AcceeCount = 0;

        DataTable dataTable = new DataTable();
        dataTable = ViewState["OldAncestralHall"] as DataTable;
        if (dataTable != null)
        {
            if (dataTable.Rows.Count > 0)
            {
                Province = StringHelper.isNumber_int(dataTable.Rows[0]["Province"].ToString());
                City = StringHelper.isNumber_int(dataTable.Rows[0]["City"].ToString());
                MusicId = StringHelper.isNumber_int(dataTable.Rows[0]["MusicId"].ToString());
                TempleId = StringHelper.isNumber_int(dataTable.Rows[0]["TempleId"].ToString());
                IsCheck = StringHelper.isNumber_int(dataTable.Rows[0]["IsCheck"].ToString());
                isDel = StringHelper.isNumber_int(dataTable.Rows[0]["isDel"].ToString());
                AcceeCount = StringHelper.isNumber_int(dataTable.Rows[0]["AcceeCount"].ToString());
                CreateTime = DateTime.Parse(dataTable.Rows[0]["CreateTime"].ToString());
                AncestralHallId = dataTable.Rows[0]["AncestralHallId"].ToString();
            }
        }

        model.Id = this.Id;
        model.AncestralHallId = AncestralHallId;
        model.Name = txtName.Text;
        model.PreName = PreName;

        if (userModel.UserId > 0)
        {
            model.CreateId = userModel.UserId;
        }
        model.Province = Province;
        model.City = City;
        model.Introduction = Introduction;
        model.MusicId = MusicId;
        model.TempleId = TempleId;
        model.CreateTime = DateTime.Now;
        model.IsCheck = IsCheck;
        model.IsDel = isDel;
        model.AcceeCount = AcceeCount;

        return model;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {

        AncestralHall_Model model = getModel();
        bll.Update(model);
        ResultStr("UpdateAncestralHall_"+this.Id+".aspx", "修改成功!");
    }
}
